QualPlot <- function(M,sampleColours = "gray",components = c(1,2),sampleLabels = NULL){
library(affy)
tempCols <- sampleColours

if(length(sampleColours) ==1){
 sampleColours <- colnames(M)
 sampleColours[] <- tempCols
} 

pcaModel <- prcomp(t(M))
E <- rowSums((pcaModel$x[,components*-1])^2)
H <- rowSums((pcaModel$x[,components])^2)
#Scores plot
x11()
plot(pcaModel)
X11()
par(bg = "white",cex.main = 0.75)
split.screen(figs = c(2,2), erase = TRUE)


screen(n=1)
plot(pcaModel$x[,components[1]],pcaModel$x[,components[2]],xlab=paste("Component ",as.character(components[1])),main = "Samples",ylab=paste("Component ",as.character(components[2])),pch = 19,col = sampleColours)
text(pcaModel$x[,components[1]],pcaModel$x[,components[2]],labels = sampleLabels,pos = 4)
abline(h = 0,v = 0)
scores.plot <- dev.cur()
abline(h = 0,v = 0)

#Sets map plot
screen(n = 2)
#Density
denPlot <- plotDensity(M,col = "gray")

#Sets map plot
screen(n = 3)
#Influence plot
plot(H,E,xlab="leverage",main = "influence",ylab="residual",pch = 19,col = sampleColours)
text(H,E,labels = sampleLabels,pos = 4)

#box plot
screen(n = 4)
A <- rowMeans(M)
plot(A,M[,1]-A,pch = 20,cex = 0.1,ylab = "",main = colnames(M)[1])

#Array plot



new.cycle = TRUE
new.task = FALSE
    

while (new.cycle == TRUE){

type <-  menu(c("scores","influence"),title = "Select mapping",graphics = FALSE)
type <- c("scores","influence")[type]
new.task = FALSE ##


    
    while (new.task == FALSE & new.cycle == TRUE){
        
 
        if (type == "scores"){
            screen(n = 1,new = F)
            ind <- identify(pcaModel$x[,components[1]],pcaModel$x[,components[2]],n = 1)
            
            if(length(ind) > 0.5){
            
                screen(n = 2)
                plotDensity(M,col = "gray",main = colnames(M)[ind])
                points(x = denPlot$all.x[,ind],y = denPlot$all.y[,ind],col = "purple",type = "l")

                screen(n = 4)
                tempCols <- sampleColours
                tempCols[ind] <- "purple"
                plot(A,M[,ind]-A,pch = 19,cex = 0.1,ylab = "",main = colnames(M)[ind])
 
                screen(n = 3)
                #Influence plot
                plot(H,E,xlab="leverage",main = "influence",ylab="residual",pch = 19,col = tempCols)
                text(H,E,labels = sampleLabels,pos = 4)

            }
            else{
                new.task = TRUE
            }
        }
 
        if (type == "influence"){
            screen(n = 3,new = F)
            ind <- identify(H,E,n = 1)
            
            if(length(ind) > 0.5){
                screen(n = 2)
                plotDensity(M,col = "gray",main = colnames(M)[ind])
                points(x = denPlot$all.x[,ind],y = denPlot$all.y[,ind],col = "red",type = "l")  
                screen(n = 4)
                tempCols <- sampleColours
                tempCols[ind] <- "purple"
                plot(A,M[,ind]-A,pch = 20,cex = 0.1,ylab = "",main = colnames(M)[ind])

                screen(n=1)
                plot(pcaModel$x[,components[1]],pcaModel$x[,components[2]],xlab=paste("Component ",as.character(components[1])),main = "Samples",ylab=paste("Component ",as.character(components[2])),pch = 19,col = tempCols)
                text(pcaModel$x[,components[1]],pcaModel$x[,components[2]],labels = sampleLabels,pos = 4)
                abline(h = 0,v = 0)
                scores.plot <- dev.cur()
                abline(h = 0,v = 0)

            }
	    else{
	        new.task = TRUE
            }
         
         }

  
     } 
} 
} 